//
//  HomeViewController.swift
//  weibo
//
//  Created by Fosen on 15/11/26.
//  Copyright © 2015年 fosen. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellId = "HomeCellId"
class HomeViewController: BaseTableViewController {

    //懒加载 数组属性
    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
        visitorView!.setWithInfo("关注一些人，回这里看看有什么惊喜", imageName: nil)
            return
        }
        
        
        prepareTableView()
        
        loadData { (statuses) -> () in
            
            guard let list = statuses else {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            self.statuses = list
            self.tableView.reloadData()
        }
    }

    
    private func loadData(finished : (statuses : [Status]?) -> ()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let AFN = AFHTTPSessionManager()
        guard let token = UserAccountViewModel().token else{
            SVProgressHUD.showErrorWithStatus("您暂未登录")
            return
        }
        let parameters = ["access_token": token]
        
        AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            guard let array = result["statuses"] as? [[String : AnyObject]] else{
                //获取到数据是=时一定要注意转换出具体的数据类型
                SVProgressHUD.showErrorWithStatus("数据类型有误")
                finished(statuses: nil)
                return
            }
            //实例化容器数组
            var list = [Status]()
            for item in array{
            
                //拿到每一个微博消息的数据
                let s = Status(dict: item)
            
                list.append(s)
                
            }
            finished(statuses: list)
//            print(result)
            }) { (_, error) -> Void in
                finished(statuses: nil)
                print(error)
        }
    }
    
  
    private func prepareTableView(){
    
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
//        tableView.rowHeight = 200
        //设置预估行高
        tableView.estimatedRowHeight = 300
        
        ////设置tableView的自动计算行高 必须对于容器视图有一个自上而下的约束
        
        //设置行高自动计算
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return statuses.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell

        cell.status = statuses[indexPath.row]

        return cell
    }

}


//extension 
